package com.bilibili.servlet;

import java.io.IOException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.bilibili.entity.User;
import com.bilibili.service.UserService;
import com.bilibili.service.impl.UserServiceImpl;

import com.fasterxml.jackson.databind.ObjectMapper;

@MultipartConfig
@WebServlet("/register.do")
public class RegisterServlet extends HttpServlet{

    protected void doPost(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException {
        
        // 设置响应内容类型为JSON
        response.setContentType("application/json;charset=UTF-8");
        
        // 创建JSON响应对象
        ObjectMapper mapper = new ObjectMapper();
        java.util.Map<String, Object> resultMap = new java.util.HashMap<>();
        
        try {
            UserService us = new UserServiceImpl();
            String username = request.getParameter("username");
            String password = request.getParameter("password");
            String realName = request.getParameter("nickName");
            String sex = request.getParameter("gender");
            String birthday = request.getParameter("birthday");
            DateFormat dft = new SimpleDateFormat("yyyy-MM-dd");
            if(sex.equals("male")) {
            	sex="1";
            }else if(sex.equals("female")) {
            	sex="2";
            }else {
            	sex="3";
            }
            int gender = Integer.parseInt(sex);
            Date br = null;
            try {
                br = dft.parse(birthday);
            } catch (ParseException e) {
                resultMap.put("success", false);
                resultMap.put("message", "日期格式错误，请使用yyyy-MM-dd格式");
                mapper.writeValue(response.getWriter(), resultMap);
                return;
            }
            
            String saveName = null;
            String visitPath = "/demo/headImg/zbw.jpg";
            if(saveName!=null){
                visitPath = "/demo/headImg/"+saveName;
            }
            
            User user = new User(username,password,realName,gender,br,1,visitPath);
            int result = us.userRegister(user);
            
            if(result == -1){
                resultMap.put("success", false);
                resultMap.put("message", "用户名已经存在");
            }else{
                resultMap.put("success", true);
                resultMap.put("message", "注册成功");
            }
            
            // 将结果转换为JSON并发送
            mapper.writeValue(response.getWriter(), resultMap);
            
        } catch (Exception e) {
            // 处理异常情况
            e.printStackTrace();
            resultMap.put("success", false);
            resultMap.put("message", "注册过程中发生错误: " + e.getMessage());
            mapper.writeValue(response.getWriter(), resultMap);
        }
    }
}
